#include "test_lbs.h"
#include "cm_uart.h"
#include "cm_demo_uart.h"
#include "stdlib.h"
#include "string.h"

static char cb_arg[30] = {0};

cm_lbs_callback test_lbs_cb(cm_lbs_callback_event_e event,cm_lbs_location_rsp_t *location,void *cb_arg)
{
    osDelay(10); 
    cm_demo_printf("lbs cb event:%d, cb arg:%s\n",event,cb_arg);
    if(CM_LBS_LOCATION_OK == event)
    {
        cm_demo_printf("lbs location platform:%d\n",location->platform);                /*!<定位所用平台*/  
        cm_demo_printf("lbs location state:%d\n",location->state);                 /*!<定位结果状态*/ 
        cm_demo_printf("lbs location longitude:%s\n",location->longitude);                /*!<经度*/
        cm_demo_printf("lbs location latitude:%s\n",location->latitude);                 /*!<纬度*/ 
        cm_demo_printf("lbs location radius:%s\n",location->radius);                   /*!<精度半径*/  
        cm_demo_printf("lbs location country:%s\n",location->country);                  /*!<国家*/  
        cm_demo_printf("lbs location province:%s\n",location->province);                 /*!<省份*/  
        cm_demo_printf("lbs location city:%s\n",location->city);                     /*!<市*/
        cm_demo_printf("lbs location citycode:%s\n",location->citycode);                 /*!<城市编码*/  
        cm_demo_printf("lbs location adcode:%s\n",location->adcode);                   /*!<区域编码*/  
        cm_demo_printf("lbs location poi:%s\n",location->poi);                      /*!<道路名称*/  
        cm_demo_printf("lbs location location_describe:%s\n",location->location_describe);        /*!<具体位置描述*/

    }
}

void cm_test_lbs_operation(unsigned char*cmd[],int len)
{
    if(0 == strncmp("INIT",cmd[2],strlen("INIT")))
    {
        //LBS1.0 研发专用：
        // 1 key:bb5331d16f46a757c2b7506109a87745
        // 1 AT:AT+MLBSCFG="method",10;+MLBSCFG="apikey","bb5331d16f46a757c2b7506109a87745"
        // 2 key:c72f62a11254fad52fc5890b3eacc224 sign:959e237c3409311ae3ebf27edc4866a0
        // 2 AT:AT+MLBSCFG="method",10;+MLBSCFG="apikey","c72f62a11254fad52fc5890b3eacc224";+MLBSCFG="signkey","959e237c3409311ae3ebf27edc4866a0";+MLBSCFG="signen",1


        // LBS2.0研发专用：
        // 1 key：d577a4352cb386a00fda4e7f4e05369f
        // 1 AT:AT+MLBSCFG="method",11;+MLBSCFG="apikey","d577a4352cb386a00fda4e7f4e05369f"
        // 2 key: 1804ca2fe7f519870be262fea7d88272  sign:9406738e068c1297924a4d7b14d05e17
        // 2 AT:AT+MLBSCFG="method",11;+MLBSCFG="apikey","1804ca2fe7f519870be262fea7d88272";+MLBSCFG="signkey","9406738e068c1297924a4d7b14d05e17";+MLBSCFG="signen",1
        cm_lbs_location_platform_e platform = atoi(cmd[3]);
        int32_t lbs_init = -1;
        if(0 == strcmp("null",cmd[6]))
        {
            cm_lbs_oneospos_attr_t oneos_attr = {   
            .pid = cmd[4],
            .time_out = atoi(cmd[5]),
            .nearbts_enable = atoi(cmd[8]),
            };
            lbs_init = cm_lbs_init(platform,&oneos_attr);
        }else
        {
            cm_lbs_amap_location_attr_t amap_attr = {   
            .api_key = cmd[4],
            .time_out = atoi(cmd[5]),
            .digital_sign_enable = atoi(cmd[6]),
            .digital_key = cmd[7],
            .nearbts_enable = atoi(cmd[8]),
            .show_fields_enable = atoi(cmd[9]),
            };
            lbs_init = cm_lbs_init(platform,&amap_attr);
        }
        cm_demo_printf("cm_lbs_init ret:%d\n",lbs_init);
        OCTEST_FUNC_EXPECT_EQUAL_INT32(0,lbs_init,cm_lbs_init);
    }else if(0 == strncmp("GET_LOC",cmd[2],strlen("GET_LOC")))
    {

        strcpy(cb_arg,cmd[3]);
        int32_t lbs_location = cm_lbs_location(test_lbs_cb,cb_arg);
        cm_demo_printf("cm_lbs_location ret:%d\n",lbs_location);
        OCTEST_FUNC_EXPECT_EQUAL_INT32(0,lbs_location,cm_lbs_location);
    }
    else if(0 == strncmp("GET_ATTR",cmd[2],strlen("GET_ATTR")))
    {
        int platform =atoi(cmd[3]);
        int32_t lbs_get_attr = -1;
        if(40 == platform)
        {
            cm_lbs_oneospos_attr_t attr ={0};
            uint8_t apikey[64] = {0};
            attr.pid = apikey;
            lbs_get_attr = cm_lbs_get_attr(platform,&attr);
            cm_demo_printf("cm_lbs_get_attr ret:%d\n",lbs_get_attr);
            OCTEST_FUNC_EXPECT_EQUAL_INT32(0,lbs_get_attr,cm_lbs_get_attr);
            cm_demo_printf("lbs attr pid:%s, time_out:%d, nearbts_enable:%d\n",attr.pid,attr.time_out,attr.nearbts_enable);
        }
        else
        {
            cm_lbs_amap_location_attr_t attr ={0};
            uint8_t apikey[64] = {0};
            attr.api_key = apikey;
            lbs_get_attr = cm_lbs_get_attr(platform,&attr);
            cm_demo_printf("cm_lbs_get_attr ret:%d\n",lbs_get_attr);
            OCTEST_FUNC_EXPECT_EQUAL_INT32(0,lbs_get_attr,cm_lbs_get_attr);
            cm_demo_printf("lbs attr api_key:%s, time_out:%d, digital_sign_enable:%d, digital_key:%s, nearbts_enable:%d, show_fields_enable:%d\n",attr.api_key,attr.time_out,attr.digital_sign_enable,attr.digital_key,attr.nearbts_enable,attr.show_fields_enable);
        }
        

    }
    else if(0 == strncmp("DEINIT",cmd[2],strlen("DEINIT")))
    {
        cm_lbs_deinit();
        cm_demo_printf("cm_lbs_deinit no ret!\n");
    }
    else{
        cm_demo_printf("param error!\n");
    }
}

 void lbsloc_cabll(cm_lbs_callback_event_e event,cm_lbs_location_rsp_t *location,void *cb_arg)
 {
    osDelay(10);
     cm_demo_printf("%s",cb_arg);
    if (event==CM_LBS_LOCATION_OK)
    {
        cm_demo_printf("lbs location platform:%d\r\n",location->platform);
        cm_demo_printf("lbs location longitude:%s\r\n",location->platform);
        cm_demo_printf("lbs location latitude:%s\r\n",location->latitude);
        cm_demo_printf("lbs location radius:%s\r\n",location->radius);
        cm_demo_printf("lbs location country:%s\n",location->country);                  /*!<国家*/  
        cm_demo_printf("lbs location province:%s\n",location->province);                 /*!<省份*/  
        cm_demo_printf("lbs location city:%s\n",location->city);                     /*!<市*/
        cm_demo_printf("lbs location citycode:%s\n",location->citycode);                 /*!<城市编码*/  
        cm_demo_printf("lbs location adcode:%s\n",location->adcode);                   /*!<区域编码*/  
        cm_demo_printf("lbs location poi:%s\n",location->poi);                      /*!<道路名称*/  
        cm_demo_printf("lbs location location_describe:%s\n",location->location_describe);        /*!<具体位置描述*/
    }if (event==CM_LBS_NET_ERR)
    {
        cm_demo_printf("net err\n");
    }if (event==CM_LBS_TIMEOUT)
    {
       cm_demo_printf("time out\n");
    }if (event==CM_LBS_KEY_ERR)
    {
      cm_demo_printf("key err\n");
    }if (event==CM_LBS_PARAM_INVALID)
    {
        cm_demo_printf("PARAM_INVALID\n");
    }if (event==CM_LBS_OVER_DAYQUTA)
    {
        cm_demo_printf("OVER_DAYQUTA\n");
    }if (event==CM_LBS_OVER_QUOTA)
    {
        cm_demo_printf("OVER_QUOTA\n");
    }if (event==CM_LBS_OVER_QPS)
    {
        cm_demo_printf("OVER_QPS\n");
    }if (event==CM_LBS_UNKNOW_ERR)
    {
        cm_demo_printf("unknow err\n");
    }

    cm_lbs_deinit();
    
    

 }

char lbs_loc_rept(void)
{
    cm_lbs_amap_location_attr_t loc_attr;
    loc_attr.api_key = "c72f62a11254fad52fc5890b3eacc224";
    loc_attr.time_out = 10;
    loc_attr.digital_key = "959e237c3409311ae3ebf27edc4866a0";
    loc_attr.digital_sign_enable = 1;
    loc_attr.nearbts_enable = 1;      //高德1.0的配置
    
    int32_t ret = cm_lbs_init(CM_LBS_PLAT_AMAP10,&loc_attr);  //选用高德定位1.0
        switch (ret)
        {
        case 0:
          cm_demo_printf("lbs init succ\n");
            break;
        
         case -1:
         cm_demo_printf("init parm err\n");
            break;

        case -2:
        cm_demo_printf("init unknow err\n");
        break;
        }
       
    int32_t ret1 = cm_lbs_location(lbsloc_cabll ,"abv");
    switch (ret1)
        {
        case 0:
          cm_demo_printf("lbs loc succ\n");
            break;
        
         case -1:
         cm_demo_printf("loc parm err\n");
            break;

         case -2:
         cm_demo_printf("loc unknow err\n");
         break;

          case -3:
         cm_demo_printf("lbs busy\n");
         break;
        }
     return 0;
    


}